package com.woniu.liuzhougov.itemQueryAndNotice.mapper;

import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.pojo.Department;
import com.woniu.liuzhougov.itemAndTask.pojo.Notice;
import com.woniu.liuzhougov.itemAndTask.pojo.UserNotice;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface NoticeMapper {

    @Insert("insert into tb_notice values (default,#{notice.user.userId},#{notice.noticeTitle},default,#{notice.noticeContent},#{user.department.departmentId})")
    public int addNotice(@Param("notice") Notice notice,@Param("user") User user);


    @Results({
            @Result(id = true, column = "notice_id", property = "noticeId"),
            @Result(column = "notice_title", property = "noticeTitle"),
            @Result(column = "notice_time",property = "noticeTime"),
            @Result(column = "notice_content",property = "noticeContent"),
            @Result(column = "notice_founder_id",property = "user" ,one=@One(
                     select = "findUserNameById"
            )),
            @Result(column = "department_id",property = "department",one = @One(
                    select = "findDepartmentById"
            ))
          }
    )
    @Select("select * from tb_notice")
    public List<Notice> findAllNotice();

    @Select("select * from tb_user where user_id = #{notice_founder_id}")
    public User findUserNameById(int notice_founder_id);


    @Insert("insert into tb_user_notice values (default,#{currentUserId},#{userId},#{userNotice.userNtItemId},#{userNotice.userNtTitle},#{userNotice.userNtContent},default)")
    public int addPersonalNotice(@Param("userNotice") UserNotice userNotice,@Param("userId") Integer userId ,@Param("currentUserId") Integer currentUserId);


    @Results({
            @Result(id=true,column = "user_nt_id",property = "userNtId"),
            @Result(column = "user_nt_item_id",property = "userNtItemId"),
            @Result(column = "user_nt_title",property = "userNtTitle"),
            @Result(column = "user_nt_content",property = "userNtContent"),
            @Result(column = "user_nt_time",property = "userNtTime"),
            @Result(column = "user_nt_recender",property = "userNtRecender",one = @One(
                    select = "findRecenderById"
            )),
            @Result(column = "user_nt_founder",property = "userNtFounder",one = @One(
                    select = "findFounderById"
            ))
    })
    @Select("select * from tb_user_notice where user_nt_recender = #{currentUserId}")
    public List<UserNotice> findPersonalNotice(Integer currentUserId);

    @Select("select * from tb_user where user_id=#{user_nt_founder}")
    public User findFounderById(Integer user_nt_founder);

    @Select("select * from tb_user where user_id=#{user_nt_recender}")
    public User findRecenderById(Integer user_nt_recender);

    @Select("select * from tb_department where department_id = #{department_id}")
    public Department findDepartmentById(Integer department_id);
   }
